package com.mdd.admin.service.order.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.service.order.IReturnCarRecordService;
import com.mdd.admin.validate.order.ReturnCarRecordCreateValidate;
import com.mdd.admin.validate.order.ReturnCarRecordSearchValidate;
import com.mdd.admin.validate.order.ReturnCarRecordUpdateValidate;
import com.mdd.admin.vo.order.ReturnCarRecordDetailVo;
import com.mdd.admin.vo.order.ReturnCarRecordListedVo;
import com.mdd.common.base.page.PageValidate;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.order.ReturnCarRecord;
import com.mdd.common.mapper.order.ReturnCarRecordMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.util.*;

/**
 * 还车记录实现类
 * @author LikeAdmin
 */
@Service
public class ReturnCarRecordServiceImpl implements IReturnCarRecordService {

    @Resource
    ReturnCarRecordMapper orderReturncarRecordMapper;

    /**
     * 还车记录列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<OrderReturncarRecordListedVo>
     */
    @Override
    public PageResult<ReturnCarRecordListedVo> list(PageValidate pageValidate, ReturnCarRecordSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<ReturnCarRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        orderReturncarRecordMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:operationType@operation_type:str",
            "=:isAmountPayment@is_amount_payment:str",
            "=:damages:str",
            "=:backOut@back_out:str",
            "=:violationFines@violation_fines:str",
            "=:trucking:str",
            "=:valueAddedServices@value_added_services:str",
        });

        IPage<ReturnCarRecord> iPage = orderReturncarRecordMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<ReturnCarRecordListedVo> list = new LinkedList<>();
        for(ReturnCarRecord item : iPage.getRecords()) {
            ReturnCarRecordListedVo vo = new ReturnCarRecordListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 还车记录详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return OrderReturncarRecord
     */
    @Override
    public ReturnCarRecordDetailVo detail(Long id) {
        ReturnCarRecord model = orderReturncarRecordMapper.selectOne(
                new QueryWrapper<ReturnCarRecord>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "还车记录数据不存在");

        ReturnCarRecordDetailVo vo = new ReturnCarRecordDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 还车记录新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Transactional
    @Override
    public void add(ReturnCarRecordCreateValidate createValidate) {
        ReturnCarRecord model = new ReturnCarRecord();
        model.setOperationType(createValidate.getOperationType());
        model.setIsAmountPayment(createValidate.getIsAmountPayment());
        model.setDamages(createValidate.getDamages());
        model.setBackOut(createValidate.getBackOut());
        model.setViolationFines(createValidate.getViolationFines());
        model.setTrucking(createValidate.getTrucking());
        model.setValueAddedServices(createValidate.getValueAddedServices());
        orderReturncarRecordMapper.insert(model);
    }

    /**
     * 还车记录编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Transactional
    @Override
    public void edit(ReturnCarRecordUpdateValidate updateValidate) {
        ReturnCarRecord model = orderReturncarRecordMapper.selectById(updateValidate.getId());

        Assert.notNull(model, "还车记录数据不存在!");

        model.setId(updateValidate.getId());
        model.setOperationType(updateValidate.getOperationType());
        model.setIsAmountPayment(updateValidate.getIsAmountPayment());
        model.setDamages(updateValidate.getDamages());
        model.setBackOut(updateValidate.getBackOut());
        model.setViolationFines(updateValidate.getViolationFines());
        model.setTrucking(updateValidate.getTrucking());
        model.setValueAddedServices(updateValidate.getValueAddedServices());
        orderReturncarRecordMapper.updateById(model);
    }

    /**
     * 还车记录删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Transactional
    @Override
    public void del(Long id) {
        ReturnCarRecord model = orderReturncarRecordMapper.selectById(id);

        Assert.notNull(model, "还车记录数据不存在!");

        orderReturncarRecordMapper.delete(new QueryWrapper<ReturnCarRecord>().eq("id", id));
    }

}
